package com.taobao.top
{
	import com.adobe.serialization.json.JSON;
	import com.taobao.top.model.Shipping;
	import com.taobao.top.resultmodel.SendShippingGetResult;
	import com.taobao.top.converter.ShippingConverter;
	/**
	 *  
	 * @author Administrator
	 * 
	 */	
	public class TOPShipping
	{
		public function TOPShipping()
		{
		}
		
		public function getSendShippings(fields:Array,
						tid:String = "",
						buyer_nick:String = "",
						status:String ="",
						seller_confirm:String ="",
						receiver_name:String = "",
						start_created:Date = null,
						end_created:Date= null,
						freight_payer:String = "",
						type:String = "",
						page_no:int = 1,
						page_size:int = 40
		):void
		{
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","taobao.shippings.send.get"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fieldsParam))];
			if(tid!= "")
			{
				reqParamArr.push(new TopRequestParam("tid",tid));
			}
			
			if(buyer_nick!= "")
			{
				reqParamArr.push(new TopRequestParam("buyer_nick",buyer_nick));
			}
			
			if(status!= "")
			{
				reqParamArr.push(new TopRequestParam("status",status));
			}
			
			if(seller_confirm!= "")
			{
				reqParamArr.push(new TopRequestParam("seller_confirm",seller_confirm));
			}
			
			if(receiver_name!= "")
			{
				reqParamArr.push(new TopRequestParam("receiver_name",receiver_name));
			}
			
			if(start_created!= null)
			{
				reqParamArr.push(new TopRequestParam("start_created",TOPUtil.getCustomDateString(start_created)));
			}
			
			if(end_created!= "")
			{
				reqParamArr.push(new TopRequestParam("end_created",TOPUtil.getCustomDateString(end_created)));
			}
			
			if(freight_payer!= "")
			{
				reqParamArr.push(new TopRequestParam("freight_payer",freight_payer));
			}
			
			if(type!= "")
			{
				reqParamArr.push(new TopRequestParam("type",type));
			}
			
			if(page_no!= 1)
			{
				reqParamArr.push(new TopRequestParam("page_no",page_no));
			}
			
			if(page_size!= 40)
			{
				reqParamArr.push(new TopRequestParam("page_size",page_size));
			}
			topURLLdr.addEventListener(Event.COMPLETE,sendShippingsGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		public function getSendShippingsFullinfo(fields:Array,
						tid:String = "",
						buyer_nick:String = "",
						status:String ="",
						seller_confirm:String ="",
						receiver_name:String = "",
						start_created:Date = null,
						end_created:Date= null,
						freight_payer:String = "",
						type:String = "",
						page_no:int = 1,
						page_size:int = 40):void
		{
			//TODO:请黑羽帮忙解释taobao.shippings.send.get 与 Taobao.shippings.send.fullinfo.get两个API的异同点
			var topURLLdr:TOPURLLoader = new TOPURLLoader();
			var reqParamArr:Array = new Array();
			reqParamArr=[new TopRequestParam("method","Taobao.shippings.send.fullinfo.get"),new TopRequestParam("fields",TOPUtil.StringArrayToStringWidthComma(fieldsParam))];
			if(tid!= "")
			{
				reqParamArr.push(new TopRequestParam("tid",tid));
			}
			
			if(buyer_nick!= "")
			{
				reqParamArr.push(new TopRequestParam("buyer_nick",buyer_nick));
			}
			
			if(status!= "")
			{
				reqParamArr.push(new TopRequestParam("status",status));
			}
			
			if(seller_confirm!= "")
			{
				reqParamArr.push(new TopRequestParam("seller_confirm",seller_confirm));
			}
			
			if(receiver_name!= "")
			{
				reqParamArr.push(new TopRequestParam("receiver_name",receiver_name));
			}
			
			if(start_created!= null)
			{
				reqParamArr.push(new TopRequestParam("start_created",TOPUtil.getCustomDateString(start_created)));
			}
			
			if(end_created!= "")
			{
				reqParamArr.push(new TopRequestParam("end_created",TOPUtil.getCustomDateString(end_created)));
			}
			
			if(freight_payer!= "")
			{
				reqParamArr.push(new TopRequestParam("freight_payer",freight_payer));
			}
			
			if(type!= "")
			{
				reqParamArr.push(new TopRequestParam("type",type));
			}
			
			if(page_no!= 1)
			{
				reqParamArr.push(new TopRequestParam("page_no",page_no));
			}
			
			if(page_size!= 40)
			{
				reqParamArr.push(new TopRequestParam("page_size",page_size));
			}
			topURLLdr.addEventListener(Event.COMPLETE,sendShippingsGetHandler);
			topURLLdr.loadData(reqParamArr);
		}
		
		
		private function sendShippingsGetHandler(event:Event):void
		{
			switch(event.type)
			{
				case Event.COMPLETE:
					var shippingResult:SendShippingGetResult = sendShippingGetJsonParser(event.target.data);
					dispatchEvent(new TOPEvent(TOPEvent.TOP_DATA,shop));
				break;
				default:
					 dispatchEvent(event.clone());
				break;
			}
		}
		
		private function sendShippingGetJsonParser(jsonStr:String ):SendShippingGetResult
		{
			if(TOPError.isError(jsonStr))
			{
				var topError:TOPError = TOPError.parseError(jsonStr);
				//dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,false,false,
			}else
			{
				var sendShippingObj:Object = JSON.decode(jsonStr);
				var sendShippingResult:SendShippingGetResult = new SendShippingGetResult();
				sendShippingResult.totalResults = sendShippingObj.totalResults;
				for (var i:int=0;i<sendShippingResult.totalResults;i++)
				{
					sendShippingResult.ship.push(ShippingConverter.convert(sendShippingObj.ship[i]));
				}
				return sendShippingResult;
			}
			return null;
		}
	}
}